/*
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Component, Input } from '@angular/core';
import { Meta, moduleMetadata, StoryObj } from '@storybook/angular';
import { action } from '@storybook/addon-actions';
import { tap } from 'rxjs/operators';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule, MatDialog } from '@angular/material/dialog';

import { GioInformationDialogComponent, GioConnectorDialogData } from './gio-information-dialog.component';
import { GioInformationDialogModule } from './gio-information-dialog.module';

import { PluginMoreInformation } from '../../../../entities/plugin/PluginMoreInformation';

@Component({
  selector: 'gio-information-dialog-story',
  template: `<button id="open-dialog" (click)="openDialog()">More information</button>`,
})
class GioInformationDialogStoryComponent {
  @Input() public name: string;
  @Input() public pluginMoreInformation: PluginMoreInformation;

  constructor(private readonly matDialog: MatDialog) {}

  public openDialog() {
    this.matDialog
      .open<GioInformationDialogComponent, GioConnectorDialogData, boolean>(GioInformationDialogComponent, {
        data: {
          name: this.name,
          information: this.pluginMoreInformation,
        },
        role: 'alertdialog',
        id: 'dialog',
      })
      .afterClosed()
      .pipe(
        tap((confirmed) => {
          action('confirmed?')(confirmed);
        }),
      )
      .subscribe();
  }
}

export default {
  title: 'Shared / Connector dialog',
  component: GioInformationDialogComponent,
  decorators: [
    moduleMetadata({
      declarations: [GioInformationDialogStoryComponent],
      imports: [GioInformationDialogModule, MatButtonModule, MatDialogModule],
    }),
  ],
  argTypes: {
    name: {
      type: { name: 'string', value: '' },
    },
    pluginMoreInformation: {
      type: { name: 'object', value: {} },
    },
  },
  render: (args) => ({
    template: `<gio-connector-dialog-story [name]="name" [pluginMoreInformation]="pluginMoreInformation"></gio-connector-dialog-story>`,
    props: { ...args },
  }),
  parameters: {
    chromatic: { delay: 1000 },
  },
} as Meta;

export const Default: StoryObj = {
  play: (context) => {
    const button = context.canvasElement.querySelector('#open-dialog') as HTMLButtonElement;
    button.click();
  },
};

Default.args = {
  name: 'HTTP GET',
  pluginMoreInformation: {
    description:
      'Let your consumers send synchronous, on-demand requests for events via a standard HTTP GET requests. Use a standard HTTP proxy to mediate between client applications as data consumers and event brokers such as Kafka.',
    documentationUrl: 'https://www.gravitee.io/plugins/gravitee-entrypoint-http-get',
    schemaImg:
      '',
  },
};
